<template>
  <div class="rule-container">
    <div class="create-container">
      <el-button type="primary" @click="addFeeRules"
        >增加停车计费规则</el-button
      >
      <el-button @click="exportToExcel">导出Excel</el-button>
    </div>
    <!-- 表格区域 -->
    <div class="table">
      <el-table :data="ruleList" style="width: 100%">
        <el-table-column label="序号" type="index" :index="indexMethod" />
        <el-table-column label="计费规则编号" prop="ruleNumber" />
        <el-table-column label="计费规则名称" prop="ruleName" />
        <el-table-column label="免费时长(分钟)" prop="freeDuration" />
        <el-table-column label="收费上限(元)" prop="chargeCeiling" />
        <el-table-column label="计费方式" prop="chargeType">
          <template #default="scope">
            {{ formatChangeType(scope.row.chargeType) }}
          </template>
        </el-table-column>
        <el-table-column label="计费规则" prop="ruleNameView" />
        <el-table-column label="操作" fixed="right" width="120">
          <template #default="scope">
            <el-button
              size="mini"
              type="text"
              @click="editRules(scope.row.id)"
              :id="id"
              >编辑</el-button
            >
            <el-button size="mini" type="text" @click="delRules(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 分页 -->
    <div style="margin-top: 10px; float: right">
      <el-pagination
        layout="prev, pager, next"
        :total="total"
        :page-size="params.pageSize"
        @current-change="currentChange"
      />
    </div>

    <!-- 添加规则 -->
    <AddRules ref="AddRules" :dialogVisible="dialogVisible"></AddRules>
  </div>
</template>

<script>
import { getRulesListAPI, delRulesAPI } from '@/api/car'
import { utils, writeFileXLSX } from 'xlsx'
import AddRules from '@/views/Car/CarRule/AddRules.vue'
export default {
  name: 'Building',
  components: { AddRules },
  data () {
    return {
      params: {
        page: 1,
        pageSize: 5
      },
      ruleList: [],
      total: 0,
      dialogVisible: false, // 控制弹框关闭打开
      id: 0 // 编辑规则id
    }
  },
  created () {
    this.getRulesList()
  },
  methods: {
    // 编辑规则
    async editRules (id) {
      this.dialogVisible = true
      this.$refs.AddRules.getParkingRulesDetail(id) // 调用子组件方法
      this.$refs.AddRules.title = '编辑规则' // 修改子组件属性
    },
    // 删除规则
    delRules (id) {
      this.$confirm('您确定要删除这条数据吗?', '温馨提示')
        .then(async () => {
          await delRulesAPI(id)
          this.$message.success('删除成功')
          this.getRulesList()
        })
        .catch(() => {})
    },
    // 添加计费规则
    addFeeRules () {
      this.dialogVisible = true
      this.$refs.AddRules.title = '添加规则'
    },
    // 导出Excel
    async exportToExcel () {
      // 获取要导出的业务数据
      const res = await getRulesListAPI(this.params)
      // 表头英文字段key
      const tableHeaderKeys = [
        'ruleNumber',
        'ruleName',
        'freeDuration',
        'chargeCeiling',
        'chargeType',
        'ruleNameView'
      ]
      // 表头中文字段value
      const tableHeaderValues = [
        '计费规则编号',
        '计费规则名称',
        '免费时长(分钟)',
        '收费上线(元)',
        '计费方式',
        '计费规则'
      ]
      // 以excel表格的顺序调整后端数据
      const list = res.data.rows.map(item => {
        const obj = {}
        tableHeaderKeys.forEach(key => {
          obj[key] = item[key]
        })
        return obj
      })
      // 创建一个工作表
      const worksheet = utils.json_to_sheet(list)
      // 创建一个新的工作簿
      const workbook = utils.book_new()
      // 把工作表添加到工作簿  Data为工作表名称
      utils.book_append_sheet(workbook, worksheet, 'Data')
      // 改写表头
      utils.sheet_add_aoa(worksheet, [tableHeaderValues], { origin: 'A1' })
      // 导出方法进行导出
      writeFileXLSX(workbook, 'SheetJSVueAoO.xlsx')
    },
    // 获取规则列表
    async getRulesList () {
      const res = await getRulesListAPI(this.params)
      console.log(res.data.rows)
      this.ruleList = res.data.rows
      this.total = res.data.total
    },
    // 分页
    currentChange (val) {
      this.params.page = val
      this.getRulesList()
    },
    // 处理序号
    indexMethod (index) {
      return (this.params.page - 1) * this.params.pageSize + index + 1
    },
    // 适配计费方式
    formatChangeType (chargeType) {
      const chargeTypeMap = {
        duration: '时长收费',
        turn: '按次收费',
        partition: '分段收费'
      }
      return chargeTypeMap[chargeType]
    }
  }
}
</script>

<style lang="scss" scoped>
.rule-container {
  padding: 20px;
  background-color: #fff;
}

.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, 0.9);
  padding-bottom: 20px;

  .search-label {
    width: 100px;
  }

  .search-main {
    width: 220px;
    margin-right: 10px;
  }
}
.create-container {
  margin: 10px 0px;
}
.page-container {
  padding: 4px 0px;
  text-align: right;
}
.form-container {
  padding: 0px 80px;
}
</style>
